package com.wsd.jing.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsd.jing.constant.SystemConstant;
import com.wsd.jing.request.CommonRequest;
import lombok.extern.slf4j.Slf4j;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.utils
 * @className: SortUtil
 * @description 查询排序字段处理工具类
 * @createDate 2022/11/11 23:27
 * @copyright 2022/11/11 23:27 www.tom.com Inc. All rights reserved.
 */
@Slf4j
public class SortUtil {

    /**
     * @param commonRequest: 排序请求参数
     * @param page:          分页查询参数
     * @param defaultField:  默认排序字段
     * @param defaultOrder:  默认排序规则
     * @return void
     * @author tom
     * @description 处理查询分页时排序字段方法
     * @createDate 2022/11/11 23:52
     */
    public static void handlePageSort(CommonRequest commonRequest, Page<?> page, String defaultField, String defaultOrder) {
        if (StrUtil.isNotBlank(commonRequest.getField()) && StrUtil.isNotBlank(commonRequest.getOrder())) {
            String field = StrUtil.toUnderlineCase(commonRequest.getField());
            if (StrUtil.equalsIgnoreCase(SystemConstant.ORDER_DESC, commonRequest.getOrder())) {
                page.addOrder(OrderItem.desc(field));
            } else {
                page.addOrder(OrderItem.asc(field));
            }
        } else {
            if (StrUtil.isNotBlank(defaultField) && StrUtil.isNotBlank(defaultOrder)) {
                defaultOrder = StrUtil.toUnderlineCase(defaultOrder);
                if (StrUtil.equalsIgnoreCase(SystemConstant.ORDER_DESC, defaultOrder)) {
                    page.addOrder(OrderItem.desc(defaultField));
                } else {
                    page.addOrder(OrderItem.asc(defaultField));
                }
            } else {
                page.addOrder(OrderItem.desc("create_time"));
            }
        }
    }

    /**
     * @param commonRequest: 排序请求参数
     * @param page:          分页查询参数
     * @return void
     * @author tom
     * @description 处理查询默认分页时排序字段方法
     * @createDate 2022/11/11 23:54
     */
    public static void handlePageSort(CommonRequest commonRequest, Page<?> page) {
        handlePageSort(commonRequest, page, null, null);
    }

    /**
     * @param commonRequest: 请求参数
     * @param queryWrapper:  列表查询条件参数
     * @param defaultField:  默认排序字段
     * @param defaultOrder:  默认排序规则
     * @return void
     * @author tom
     * @description 处理查询列表时排序字段方法
     * @createDate 2022/11/12 11:17
     */
    public static void handleListSort(CommonRequest commonRequest, QueryWrapper<?> queryWrapper, String defaultField, String defaultOrder) {
        if (StrUtil.isNotBlank(commonRequest.getField()) && StrUtil.isNotBlank(commonRequest.getOrder())) {
            String field = StrUtil.toUnderlineCase(commonRequest.getField());
            if (StrUtil.equalsIgnoreCase(SystemConstant.ORDER_DESC, commonRequest.getOrder())) {
                queryWrapper.orderByDesc(field);
            } else {
                queryWrapper.orderByAsc(field);
            }
        } else {
            if (StrUtil.isNotBlank(defaultField) && StrUtil.isNotBlank(defaultOrder)) {
                defaultOrder = StrUtil.toUnderlineCase(defaultOrder);
                if (StrUtil.equalsIgnoreCase(SystemConstant.ORDER_DESC, defaultOrder)) {
                    queryWrapper.orderByDesc(defaultField);
                } else {
                    queryWrapper.orderByAsc(defaultField);
                }
            } else {
                queryWrapper.orderByDesc("create_time");
            }
        }
    }

    /**
     * @param commonRequest: 请求参数
     * @param queryWrapper:  列表查询条件参数
     * @return void
     * @author tom
     * @description 处理查询列表时默认排序字段方法
     * @createDate 2022/11/12 11:20
     */
    public static void handleListSort(CommonRequest commonRequest, QueryWrapper<?> queryWrapper) {
        handleListSort(commonRequest, queryWrapper, null, null);
    }
}
